package com.zany.children.action;

import com.zany.children.service.PaymentProjectService;
import com.zany.children.vo.Class;
import com.zany.children.vo.PaymentProject;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JsonTools;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缴费项目维护.
 */
public class PaymentProjectActioin extends AbstractActionSupport {
    private static Log logger = LogFactory.getLog(PaymentProjectActioin.class);

    private Map session;

    private PaymentProjectService paymentProjectService;

    public PaymentProjectService getPaymentProjectService() {
        return paymentProjectService;
    }

    public void setPaymentProjectService(PaymentProjectService paymentProjectService) {
        this.paymentProjectService = paymentProjectService;
    }

    public void list() throws ZanyException {
        String start = request.getParameter("start");
        String name = request.getParameter("name");
        UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
        response = ServletActionContext.getResponse();
        String pageStart = StringUtils.isBlank(start) ? "1" : start;
        PaymentProject paymentProject = new PaymentProject();
        paymentProject.setName(name);
        if (userInfo != null) {
            paymentProject.setOrgCode(userInfo.getUserOrg());
        }
        paymentProject.setPageMessage(Integer.valueOf(pageStart));
        Integer pageTotal = paymentProjectService.getPaymentProjectListPageCount(paymentProject);
        List<PaymentProject> paymentProjectListPage = paymentProjectService.getPaymentProjectListPage(paymentProject);
        AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(paymentProjectListPage) + "}", response);
    }

    public void save() throws ZanyException {
        String name = request.getParameter("name");
        String cycle = request.getParameter("cycle");
        String preMoney = request.getParameter("preMoney");
        response = ServletActionContext.getResponse();
        PaymentProject paymentProject = new PaymentProject();
        try {
            paymentProject.setPreMoney(Double.parseDouble(preMoney));
            paymentProject.setName(name);
            paymentProject.setCycle(cycle);
            UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
            if (userInfo != null) {
                paymentProject.setOrgCode(userInfo.getUserOrg());
                paymentProject.setCreater(userInfo.getUserName());
            }
            Integer count = paymentProjectService.getPaymentProjectListPageCount(paymentProject);
            if (count != 0) {
                AjaxTools.exAjax("{\"success\":false,msg:\"缴费项目已存在！\"}", response);
            } else {
                paymentProjectService.savePaymentProject(paymentProject);
                AjaxTools.exAjax("{success:true}", response);
            }
        } catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT, e.getMessage(), e);
        }
    }

    public void update() throws ZanyException {
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        String cycle = request.getParameter("cycle");
        String preMoney = request.getParameter("preMoney");
        response = ServletActionContext.getResponse();
        PaymentProject paymentProject = new PaymentProject();
        try {
            paymentProject.setName(name);
            UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
            if (userInfo != null) {
                paymentProject.setOrgCode(userInfo.getUserOrg());
            }
            paymentProject.setId(Long.parseLong(id));
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("name",name);
            param.put("orgCode",userInfo.getUserOrg());
            List<PaymentProject> projects = paymentProjectService.getPaymentProject(param);
            if(projects!=null&&projects.size()>0) {
                PaymentProject project = projects.get(0);
                if(project.getId()!=Long.parseLong(id)){
                    AjaxTools.exAjax("{\"success\":false,msg:\"缴费项目已存在！\"}", response);
                    return;
                }
            }
            paymentProject.setPreMoney(Double.parseDouble(preMoney));
            paymentProject.setName(name);
            paymentProject.setCycle(cycle);
            paymentProjectService.updatePaymentProject(paymentProject);
            AjaxTools.exAjax("{success:true}", response);

        } catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT, e.getMessage(), e);
        }

    }

    public void delete() throws ZanyException {
        if(logger.isInfoEnabled()){logger.info("PaymentProjectActioin.delete()");}
        response = ServletActionContext.getResponse();
        String delId[] = request.getParameterValues("delId");
        try{
            if(delId.length>0){
                for (int i = 0; i < delId.length; i++){
                    paymentProjectService.deletePaymentProjectById(Long.parseLong(delId[i]));
                }
                AjaxTools.exAjax("{success:true}", response);
            }
        }catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
        }
    }

    @Override
    public void setSession(Map map) {
        this.session = map;
    }
}
